package com.wujilee.wujileenetty.demos.web.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;

/**
 * @author liwuji
 * @date 2024/7/30
 * @desc
 **/
public class NioClient {
    public static void main(String[] args) throws IOException, InterruptedException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
        if (!socketChannel.connect(inetSocketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("由于非阻塞，连接时可以做其他的事情");
            }
        }
        ByteBuffer helloWorld = Charset.defaultCharset().encode("hello world");
        ByteBuffer wrap = ByteBuffer.wrap("Hello world".getBytes());
        socketChannel.write(helloWorld);
        Thread.sleep(1000L * 60);
    }
}
